package com.wm.practice;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @ClassName: Case
 * @Description: 比价网站case
 * @Author: Deamer
 * @Date: 2022/9/3 15:18
 **/
public class Case {
    static List<NetMall> list = Arrays.asList(
            new NetMall("jd"),
            new NetMall("dangdang"),
            new NetMall("taobao")
    );

    // 获取全部指定的商城内指定商品的价格列表
    public static List<String> getPrice(List<NetMall> list, String productName) {
        //流式计算做了映射（利用map），希望出来的是有格式的字符串（利用String.format）,%是占位符
        return list.stream().map(netMall -> String.format(productName + " in %s price is %.2f",
                netMall.getNetMallName(),//第一个%
                netMall.calPrice(productName)))//第二个%
                .collect(Collectors.toList());
    }

    // 从功能到性能
    public static List<String> getPriceByCompletableFuture(List<NetMall> list, String productName) {
        // 流式计算做了映射（利用map），希望出来的是有格式的字符串（利用String.format）,%是占位符
        return list.stream().map(netMall -> CompletableFuture.supplyAsync(() ->
                String.format(productName + " in %s price is %.2f",
                        netMall.getNetMallName(),
                        netMall.calPrice(productName))))// Stream<CompletableFuture<String>>
                .collect(Collectors.toList())// List<CompletablFuture<String>>
                .stream()// Stream<CompletableFuture<String>
                .map(s -> s.join()) // Stream<String>
                .collect(Collectors.toList());
    }


    /**
     * mysql in jd price is 110.20
     * mysql in dangdang price is 109.55
     * mysql in taobao price is 109.21
     * --普通版--当前操作花费时间----costTime:3118毫秒
     * --------------------------分隔-------------------------------
     * mysql in jd price is 109.66
     * mysql in dangdang price is 109.25
     * mysql in taobao price is 109.33
     * --性能版--当前操作花费时间----costTime:1042毫秒
     *
     * @param args
     */
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        List<String> priceList = getPrice(list, "mysql");
        for (String price : priceList) {
            System.out.println(price);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("--普通版--当前操作花费时间----costTime:" + (endTime - startTime) + "毫秒");
        System.out.println("--------------------------分隔-------------------------------");
        startTime = System.currentTimeMillis();
        List<String> priceList2 = getPriceByCompletableFuture(list, "mysql");
        for (String price : priceList2) {
            System.out.println(price);
        }
        endTime = System.currentTimeMillis();
        System.out.println("--性能版--当前操作花费时间----costTime:" + (endTime - startTime) + "毫秒");
    }
}


class NetMall {

    private String netMallName;

    public String getNetMallName() {
        return netMallName;
    }

    public void setNetMallName(String netMallName) {
        this.netMallName = netMallName;
    }

    public NetMall(String netMallName) {
        this.netMallName = netMallName;
    }

    public double calPrice(String productName) {

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ThreadLocalRandom.current().nextDouble() * 2 + productName.charAt(0);
    }
}
